package com.atguigu.day05;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

//volatile重大特性——————不保证原子性！！！
//资源类
class Resource{
    public static volatile int num=0;
    public void add(){
        num++;
    }
}

public class VolatileNotAtomicDemo {
    public static void main(String[] args) throws InterruptedException {
        final int test=200;
        Resource resource = new Resource();
        CountDownLatch countDownLatch = new CountDownLatch(test);

        for (int i = 0; i < test; i++) {
            new Thread(() -> {
                for (int j = 0; j < test; j++) {
                    resource.add();
                }
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        //线程睡眠几秒
//        try {
//            TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {throw new RuntimeException(e);}
            countDownLatch.await();

        System.out.println(Thread.currentThread().getName()+"\t"+Resource.num);

    }

}
